package Boot;

import common.Exception.Results;
import contorller.AdminController;
import contorller.AuthorController;
import dto.req.*;
import dto.resp.AdminLoginRespDTO;
import dto.resp.AuthorCommentRespDTO;
import dto.resp.AuthorLoginRespDTO;
import dto.resp.QueryOneArticleByIdRespDTO;
import service.CommentArticleService;

import java.util.ArrayList;
import java.util.Scanner;

public class UserApp {
    private Integer authorId;
    private String name;
    private String username;
    private boolean isLogin;
    private AuthorController authorController;
    private CommentArticleService commentArticleService;
    private Scanner scanner;

    public UserApp() {
        authorController = new AuthorController();
        isLogin = false;
        scanner = new Scanner(System.in);
        commentArticleService = new CommentArticleService();
    }

    public void run() {
        int i = 0;
        do {
            LoginMenu();
            i = scanner.nextInt();
            switch (i){
                case 1:
                {
                    login();
                    break;
                }
                case 2:
                {
                    register();
                    break;
                }
                case 3:
                    break;
                default:
                    System.out.println("输入错误请重新输入");
            }

        }while(i != 3);
    }

    public void LoginMenu(){
        System.out.println("-----------用户系统-------");
        System.out.println("1.登录");
        System.out.println("2.注册");
        System.out.println("3.返回上一层");
        System.out.println("--------------------------");
    }


    private void login(){
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入你的登录名(大于6个字符)： ");
        String username = sc.next();
        if(username.length() < 6){
            System.out.println("登录名长度小于6个字符");
            return;
        }
        System.out.print("请输入你的登录密码(大于6个字符)： ");
        String password = sc.next();
        if(password.length() < 6){
            System.out.println("密码长度小于6个字符");
            return;
        }
        //调用Controller登录
        AuthorLoginReqDTO author = new AuthorLoginReqDTO();
        author.setUsername(username);
        author.setPassword(password);
        Results<AuthorLoginRespDTO> result = authorController.userLogin(author);
        if(!result.isSuccess()){
            System.out.println(result.getMessage());
            return;
        }
        //登录成功
        AuthorLoginRespDTO authorLoginRespDTO = result.getData();
        if(result.getData() == null){
            System.out.println(result.getMessage());
            return;
        }
        this.name = authorLoginRespDTO.getName();
        this.isLogin = true;
        this.username = username;
        this.authorId = authorLoginRespDTO.getId();

        //进入功能菜单
        fun();
    }

    private void register(){
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入你的登录名(大于6个字符)： ");
        String username = sc.next();
        if(username.length() < 6){
            System.out.println("登录名长度小于6个字符");
            return;
        }
        System.out.print("请输入你的登录密码(大于6个字符)： ");
        String password = sc.next();
        if(password.length() < 6){
            System.out.println("密码长度小于6个字符");
            return;
        }
        System.out.print("请输入你的昵称(不能为空): ");
        String name = sc.next();
        if(name.length() < 1){
            System.out.println("昵称不能为空");
            return;
        }
        //调用Controller注册
        AuthorRegisterReqDTO authorRegisterReqDTO = new AuthorRegisterReqDTO();
        authorRegisterReqDTO.setName(name);
        authorRegisterReqDTO.setUsername(username);
        authorRegisterReqDTO.setPassword(password);
        Results results = authorController.addUser(authorRegisterReqDTO);
        //注册失败
        if(!results.isSuccess()){
            System.out.println(results.getMessage());
            return;
        }

        //注册成功
        System.out.println(results.getMessage());
    }

    private void fun(){
        if(!isLogin){
            System.out.println("登录已失效,请重新登录");
            return;
        }
        //1.审核功能
        //2.删除文章功能
        int i = 0;
        do {
            System.out.println("-----------------------------------");
            System.out.println("-----功能菜单-----用户: " + username);
            System.out.println("1.发布文章");
            System.out.println("2.编辑文章");
            System.out.println("3.查看自己所有文章");
            System.out.println("4.查看指定用户所有文章");
            System.out.println("5.查看指定文章");
            System.out.println("6.删除文章");
            System.out.println("7.退出");
            System.out.println("-----------------------------------");
            i  = this.scanner.nextInt();
            switch (i){
                case 1:
                    publishArticle();
                    break;
                case 2:
                    editArtile();
                    break;
                case 3:
                    queryAllArticleBySelf();
                    break;
                case 4:
                    queryOtherAuthorAllArticle();
                    break;
                case 5:
                    queryArticleById();
                    break;
                case 6:
                    deleteArticle();
                    break;
                case 7:
                    break;
                default:
                    System.out.println("输入无效");
                    break;
            }
        }while(i != 7);
    }


    private void deleteArticle() {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入要删除的文章编号:");
        int articleId = sc.nextInt();
        Results results = authorController.deleteArticle(articleId, this.authorId);
        if(!results.isSuccess()){
            System.out.println(results.getMessage());
            return;
        }
        System.out.println(results.getMessage());
    }

    private void queryArticleById() {
        System.out.print("输入要查看的文章Id: ");
        int articleId = scanner.nextInt();
        Results<QueryOneArticleByIdRespDTO> results = authorController.queryOneArticleById(articleId);
        if(results.getData() == null || !results.isSuccess()){
            System.out.println(results.getMessage());
            return;
        }
        QueryOneArticleByIdRespDTO article = results.getData();
        //点赞和评论功能 TODO
        int idx = 0;
        do {
            printArticle(article);
            System.out.println("-----------------");
            System.out.println("1.点赞");
            System.out.println("2.评论");
            System.out.println("3.退出");
            System.out.println("------------------");
            idx = scanner.nextInt();
            switch (idx){
                case 1:
                    clickFavorite(articleId);
                    break;
                case 2:
                    commentArticle(articleId);
                case 3:
                    break;
                default:
                    System.out.println("输入错误");
                    break;
            }
        }while (idx!=3);

    }

    private void commentArticle(Integer articleId) {
        if(authorId == null){
            System.out.println("用户不存在");
            return;
        }
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入评论的内容:");
        String comment = sc.next();
        if(comment.length() < 1){
            System.out.println("评论不能为空");
            return;
        }
        AuthorCommentReqDTO request = new AuthorCommentReqDTO();
        request.setArticleId(articleId);
        request.setAuthorId(authorId);
        request.setComment(comment);
        Results results = authorController.commentArticle(request);
        //评论失败
        if(!results.isSuccess()){
            System.out.println(results.getMessage());
            return;
        }
        //评论成功
        System.out.println(results.getMessage());
    }

    private void clickFavorite(Integer articleId) {
        if(authorId == null || articleId == null){
            System.out.println("用户或文章不存在");
            return;
        }
        Results results = authorController.clickFavorite(articleId, authorId);
        //点赞出现异常？
        if(!results.isSuccess()) {
            System.out.println(results.getMessage());
            return;
        }
        //点赞成功
        System.out.println(results.getMessage());
    }

    /**
     * 打印文章方法
     */
    private void printArticle(QueryOneArticleByIdRespDTO article){
        System.out.println("标题： " + article.getTitle());
        System.out.println("正文: " + article.getContext()) ;
        //作者暂时没有返回，后续可以考虑可以实现
        // System.out.println("作者： " + article.);
        System.out.println("访客: " + article.getVisitor() + "\t点赞: " + article.getClickFavorite());

        System.out.println("发布日期: " + article.getCreateDate() + "\t\t上次编辑日期: " + article.getUpdateDate());
        System.out.println("-----------评论区-------------");
        //TODO 显示该文章下 所有评论
       Results<ArrayList<AuthorCommentRespDTO>> arrayListResults = commentArticleService.showAllCommentByArticleId(article.getId());
//        if(arrayListResults.isSuccess() && arrayListResults.getData() == null &&  arrayListResults.getData().isEmpty() ){
//            System.out.println("暂无评论");
//            return;
//        }
        if(!arrayListResults.isSuccess() || arrayListResults.getData() == null){
            System.out.println("暂无评论");
            return;
        }
        ArrayList<AuthorCommentRespDTO> commentDatas = arrayListResults.getData();
        for(AuthorCommentRespDTO commentData : commentDatas){
            System.out.println(commentData.getName() + "    " + commentData.getDate().toString());
            System.out.println(commentData.getComment());
            System.out.println("---");
        }
        System.out.println();
    }

    private void queryAllArticleByAuthorId(Integer id) {
        Results<ArrayList<QueryOneArticleByIdRespDTO>> results = authorController.queryAllArticleByAuthorId(id);
        if(!results.isSuccess()){
            System.out.println(results.getMessage());
            return;
        }
        ArrayList<QueryOneArticleByIdRespDTO> data = results.getData();
        System.out.println("编号\t\t标题\t\t状态");
        for(QueryOneArticleByIdRespDTO article : data){
            System.out.println(article.getId() + "\t\t" + article.getTitle() + "\t\t" + ((article.getIsView() == 0) ? "未审核" :"已审核"));
        }
    }

    private void queryOtherAuthorAllArticle() {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入要查看的作者ID: ");
        int id = sc.nextInt();
        queryAllArticleByAuthorId(id);
    }


    private void queryAllArticleBySelf() {
        queryAllArticleByAuthorId(this.authorId);
    }

    private void editArtile() {
        System.out.println("请选择: 1.编辑标题  2.编辑正文");
        int select = scanner.nextInt();
        if(select == 1 || select == 2){
            Scanner sc= new Scanner(System.in);
            System.out.print("请输入要修改的文章编号: ");
            int articleId = scanner.nextInt();
            if(select == 1){
                System.out.print("请输入新标题: ");
                String newTitle = sc.next();
                if(newTitle.length() < 6){
                    System.out.println("标题长度不得小于6");
                    return;
                }
                UpdateArticleTitleReqDTO  updateArticleTitleReqDTO = new UpdateArticleTitleReqDTO();
                updateArticleTitleReqDTO.setArticleId(articleId);
                updateArticleTitleReqDTO.setAuthorId(this.authorId);
                updateArticleTitleReqDTO.setNewTitle(newTitle);
                Results  result = authorController.updateArticleTitle(updateArticleTitleReqDTO);
                System.out.println(result.getMessage());
            }else {
                System.out.print("请输入新正文: ");
                String newContext = sc.next();
                if(newContext.length() < 10){
                    System.out.println("正文长度不得小于10个字符");
                    return;
                }
                UpdateArticleContextReqDTO  request = new UpdateArticleContextReqDTO();
                request.setArticleId(articleId);
                request.setAuthorId(this.authorId);
                request.setNewContext(newContext);
                Results result = authorController.updateArticleContext(request);
                System.out.println(result.getMessage());

            }
        } else {
            System.out.println("编辑失败");
            return;
        }
    }

    private void  publishArticle(){
        System.out.print("请输入文章标题: ");
        Scanner sc = new Scanner(System.in);
        String title = sc.next();
        if(title.length() < 6){
            System.out.println("标题不得小于六个字符");
            return;
        }
        System.out.print("请输入正文: ");
        String context = sc.next();
        if(context.length() < 10){
            System.out.println("正文不得小于六个字符");
            return;
        }
        AuthorPublishArticleReqDTO articleReqDTO = new AuthorPublishArticleReqDTO();
        articleReqDTO.setTitle(title);
        articleReqDTO.setAuthorId(authorId);
        articleReqDTO.setContext(context);
        authorController.addArticle(articleReqDTO);
    }
}
